package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.result.Result;
import com.example.mapper.KucunMapper;
import com.example.service.KucunService;
import com.example.entity.dto.ShaomarukuDto;
import com.example.entity.dto.ShaomiaoDTO;
import com.example.entity.mes.INVML;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class KuncunServiceImpl implements KucunService {
    @Autowired
    private KucunMapper kucunMapper;


    @Override
    public Result updatekuwei(ShaomiaoDTO shaomiaoDTO) throws IllegalArgumentException {
        try {
            List<ShaomarukuDto> shaomarukuDtoList = shaomiaoDTO.getList_shaomarukuDto();
            if (shaomarukuDtoList.isEmpty()) {
                return Result.fail("你没有传递任何数据哦！");
            }
            for (ShaomarukuDto shaomarukuDto : shaomarukuDtoList) {
                if (shaomarukuDto.getML001() == null || shaomarukuDto.getML001().isEmpty() || shaomarukuDto.getEML003() == null || shaomarukuDto.getEML003().isEmpty() || shaomarukuDto.getNML003() == null || shaomarukuDto.getNML003().isEmpty()) {
                    return Result.fail("品号和旧库位和新库位不能为空！" + shaomarukuDto.toString());
                }
                INVML yinvml = kucunMapper.selectOne(new QueryWrapper<INVML>()
                        .eq("ML001", shaomarukuDto.getML001())
                        .eq("ML003", shaomarukuDto.getEML003()));
                if (yinvml==null){
                    return Result.fail("你传入的品号及原库位，数据库中不存在！");
                }
                INVML jinvml = new INVML();
                QueryWrapper<INVML> nkk = new QueryWrapper<>();
                nkk.eq("ML001", shaomarukuDto.getML001());
                nkk.eq("ML003", shaomarukuDto.getNML003());
                if (shaomarukuDto.getML002()!= null) {
                    nkk.eq("ML002", shaomarukuDto.getML002());
                }
                LocalDateTime localDateTime = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
                String formattedDateTime = localDateTime.format(formatter);
                INVML ninvml = kucunMapper.selectOne(nkk);
                if (ninvml!= null) {
                    ninvml.setMl005(ninvml.getMl005() + Double.parseDouble(shaomarukuDto.getMovenumber()));
                    ninvml.setMl009(formattedDateTime);
                    kucunMapper.update(ninvml, nkk);
                } else {
                    // 当 ninvml 为 null 时，创建一个新的 INVML 实例
                    ninvml = new INVML();
                    BeanUtils.copyProperties(yinvml, ninvml);
                    ninvml.setMl005(Double.parseDouble(shaomarukuDto.getMovenumber()));
                    ninvml.setMl003(shaomarukuDto.getNML003());
                    if (shaomarukuDto.getML002()!= null) {
                        ninvml.setMl002(shaomarukuDto.getML002());
                    }
                    ninvml.setMl009(formattedDateTime);
                    kucunMapper.insert(ninvml);
                }
                BeanUtils.copyProperties(yinvml, jinvml);
                if (!shaomarukuDto.getMovenumber().isEmpty()) {
                    try {
                        double moveNumber = Double.parseDouble(shaomarukuDto.getMovenumber());
                        jinvml.setMl005(jinvml.getMl005() - moveNumber);
                        jinvml.setMl010(formattedDateTime);
                        kucunMapper.update(jinvml, new QueryWrapper<INVML>()
                                .eq("ML001", shaomarukuDto.getML001())
                                .eq("ML003", shaomarukuDto.getEML003()));
                    } catch (NumberFormatException e) {
                        return Result.fail("转移数量格式错误，请输入有效的数字");
                    }
                }
            }
            return Result.ok();
        } catch (NumberFormatException e) {
            return Result.fail("你传递进来的转移数量不能转换成 Double 类型的数据，请重新传递！");
        }
    }
}
